package logic

import (
	"context"
	"errors"
	"video-guard/models/mysql"

	"video-guard/videorpc/internal/svc"
	"video-guard/videorpc/videorpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type VideoDelLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewVideoDelLogic(ctx context.Context, svcCtx *svc.ServiceContext) *VideoDelLogic {
	return &VideoDelLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *VideoDelLogic) VideoDel(in *videorpc.VideoDelRequest) (*videorpc.VideoDelResponse, error) {
	// todo: add your logic here and delete this line
	var user mysql.UserInfo
	err := user.FindUserById(mysql.DB, in.UserId)
	if err != nil {
		return nil, errors.New("未登录")
	}
	var video mysql.VideoInfo
	err = video.FindVidepByVideoId(mysql.DB, in.VideoId)
	if err != nil {
		return nil, errors.New("未有视频，不能删除")
	}
	video.VideoStatus = 2
	err = video.VideoUpdate(mysql.DB)
	if err != nil {
		return nil, err
	}
	err = video.VideoDel(mysql.DB, in.VideoId)
	if err != nil {
		return nil, errors.New("删除失败")
	}
	return &videorpc.VideoDelResponse{}, nil
}
